<template>
    <section class="checkProjectContent">
        <div class="checkProjectForm" >
            <el-form ref="form" :model="form" :rules="rules"  label-width="180px"  v-loading="formLoad">

                <el-form-item label="为民实事工程总额" prop="projectAmount">
                    <el-input v-model="form.projectAmount" :disabled="!disabled"></el-input>
                </el-form-item>
                <el-form-item v-for="item in scoreForm" :label="item.quotaName">
                    <el-input v-model="item.score" :disabled="!disabled"></el-input>
                </el-form-item>
                <el-form-item label="总额佐证材料" prop="projectAmountSupportDoc">
                    <FileUpload :value="form.projectAmountSupportDoc" :disabled="!disabled" @getValue="form.projectAmountSupportDoc = $event"></FileUpload>
                </el-form-item>
                <el-form-item v-if="disabled">
                    <el-button type="primary" @click="submit('form')">保存</el-button>
                </el-form-item>
            </el-form>
        </div>
    </section>
</template>

<script>
    import FileUpload from '@/components/FileUpLoad';
    export default {
        name: "CheckProjectContent",
        props:{
            currentTask:{
                type:Object,
            },
            currentCun:{
                type:String,
            },
            currentStatus:{
                type:Object
            }
        },
        data(){
            return{
                form:{},
                rules:{
                    projectAmount: [
                        { required: true, trigger: 'blur', message: '为民实事工程总额'},
                    ],
                    projectAmountSupportDoc: [
                        {required: true, validator: (rule, value, callback) => {
                                if (!this.form[rule.field]) {
                                    callback(new Error('请上传总额佐证材料！'));
                                } else {
                                    callback();
                                    return true;
                                }
                            }, trigger: 'change'},
                    ],
                },
                formLoad:false,
                scoreForm:[],
                disabled:false,
            }
        },
        watch:{
            'currentCun':{
                handler:function () {
                    this.showCurrentForm(this.currentCun);
                }
            },
            'currentStatus':{
                handler:function(){
                    this.disabled = this.currentTask.hasCheckTask&&this.currentStatus.status==0;
                }
            },
            'currentTask':{
                handler:function(){
                    this.disabled = this.currentTask.hasCheckTask&&this.currentStatus.status==0;
                }
            }
        },
        components:{
            FileUpload
        },
        methods:{
            showCurrentForm(districtId){
                this.formLoad = true;
                this.form = {};
                this.scoreForm = [];
                this.$http('POST',`identity/detectionIndex/list`,{taskId:this.currentTask.taskId,districtId:districtId},false).then(data=>{
                    this.form = data[0];
                    this.$http('GET',`identity/kPITownQuota/getAllByVillageId/${districtId}/0304`,false).then(data=>{
                        this.scoreForm = data;
                        this.formLoad = false;
                    }).catch(()=>{
                        this.formLoad = false;
                    });
                }).catch(()=>{
                    this.formLoad = false;
                });

            },
            submit(form){
                this.formLoad = true;
                this.$http('PUT',`identity/detectionIndex/${this.form.id}id`,this.form,false).then(data=>{
                    this.$http('POST',`identity/kPITownQuota/updateStatisticsQoutaScore`,this.scoreForm,false).then(data=>{
                        this.formLoad = false;
                        this.$message({
                            type:'success',
                            message:'保存成功'
                        })
                    }).catch(()=>{
                        this.formLoad = false;
                    })
                }).catch(()=>{
                    this.formLoad = false;
                });
            }
        },
    }
</script>

<style scoped>
    .checkProjectContent .checkProjectForm{
        width: 500px;
        text-align: left;
    }

</style>
